package ca.abubakrsagar.breadboardsim;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.JComponent;

/**
 * A subclass of the Component superclass, the LED class is used to represent a
 * Light Emitting Diode. An LED object can be created
 * 
 * @author Sagar Desai, Mohammad Abubakr
 * @since May 29, 2012
 * 
 */
public class LED extends Component {
	private Color ledColor; // The color of the LED.
	private boolean isOn; // Boolean value used to keep track of the state of
							// the LED.

	/**
	 * Creates a new LED object while setting its color with the given color and
	 * setting the component type to be an LED.
	 * 
	 * @param ledColor
	 *            The color of the LED
	 */
	public LED(Color ledColor) {
		this.ledColor = ledColor;
		this.setVisible(true);
		this.setPreferredSize(new Dimension(24, 20));
		setComponentType(0);
	}

	/**
	 * Draws the LED component. The LED consists of an oval with one flat side
	 * to distinguish between the positive and negative ends.
	 * 
	 * @param g
	 *            The graphics object
	 */
	public void paintComponent(Graphics g) {

		// Use the Graphics2D object instead of the graphics object.
		// Setup the G2 object to use anti aliasing -- allows the drawings to be
		// smooth.
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		// Draw an LED with the given color.
		g2.setColor(ledColor);
		g2.clipRect(5, 0, 24, 20);
		g2.fillOval(0, 0, 24, 20);
	}

	/**
	 * @return The color of the LED.
	 * 
	 */
	public Color getLedColor() {
		return ledColor; // returns the LED color
	}

	/**
	 * Sets the color of the LED to the specified color
	 * 
	 * @param ledColor
	 *            The color of the LED
	 */
	public void setLedColor(int r, int g, int b) {
		this.ledColor = new Color(r,g,b); // sets LED color to color specified
	}

}
